package cn.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yunhe.biz.IMajorsBiz;
import cn.yunhe.biz.MajorsBizImpl;
import cn.yunhe.entity.Majors;

/**
 * Servlet implementation class DoUser
 */
public class DoMajors extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoMajors() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		PrintWriter out =response.getWriter();
		IMajorsBiz imb = new MajorsBizImpl();
		
		String opt = request.getParameter("opt");
		if("add".equals(opt)){
			String majors = request.getParameter("majors");
			String semployement_rate = request.getParameter("employement_rate");
			String smajors_number = request.getParameter("majors_number");
			
			int employement_rate = Integer.parseInt(semployement_rate);
			int majors_number = Integer.parseInt(smajors_number);
			
			
			Majors m = new Majors();
			m.setMajors(majors);
			m.setEmployment_rate(employement_rate);
			m.setMajors_number(majors_number);
			
			boolean r = imb.addMajors(m);
			response.sendRedirect("DoBackstage?opt=getMajors");
		}else if("update".equals(opt)){
			String smajorsid = request.getParameter("majorsid");
			String majors = request.getParameter("majors");
			String semployement_rate = request.getParameter("employement_rate");
			String smajors_number = request.getParameter("majors_number");
			
			int majorsid = Integer.parseInt(smajorsid);
			int employement_rate = Integer.parseInt(semployement_rate);
			int majors_number = Integer.parseInt(smajors_number);
			
			Majors m = new Majors();
			m.setMajorsid(majorsid);
			m.setMajors(majors);
			m.setEmployment_rate(employement_rate);
			m.setMajors_number(majors_number);
			
			boolean r= imb.editMajorsById(m);
			response.sendRedirect("DoBackstage?opt=getMajors");
		}else if("del".equals(opt)){
			String majorsid = request.getParameter("majors");
			int mid = Integer.parseInt(majorsid);
			boolean r = imb.delMajorsById(mid);
			response.sendRedirect("DoBackstage?opt=getMajors");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
